package com.tanhua.db.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.api.UserInfoApi;
import com.tanhua.db.mapper.BlackListMapper;
import com.tanhua.db.mapper.UserInfoMapper;
import com.tanhua.model.dto.RecommendUserDto;
import com.tanhua.model.pojo.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlackListMapper blackListMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public void updateById(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    //根据用户id  分页查询黑名单用户的详情
    @Override
    public Page<UserInfo> findByPage(Long userId, Integer page, Integer pagesize) {
        //1 创建分页对象
        Page<UserInfo> pages = new Page<>(page, pagesize);

        //2 执行分页查询
        pages = blackListMapper.findBlackList(pages, userId);

        //3 返回结果
        return pages;
    }

    //根据 推荐好友ids 和  查询条件  分页   查询用户详情 UserInfo
    @Override
    public Page<UserInfo> findByDto(List<Long> ids, RecommendUserDto dto) {
        //1 封装分页对象
        Page<UserInfo> pages = new Page<>(dto.getPage(), dto.getPagesize());

        //2 封装查询条件   条件有可能为空
        LambdaQueryWrapper<UserInfo> wrapper = Wrappers.lambdaQuery(UserInfo.class)
                .in(UserInfo::getId, ids)//设置推荐好友的ids作为条件
                .eq(StringUtils.isNotBlank(dto.getGender()), UserInfo::getGender, dto.getGender())//根据性别查询
                .lt(dto.getAge() != null, UserInfo::getAge, dto.getAge());//查询小于指定的年龄
        //.like(StringUtils.isNotBlank(dto.getCity()), UserInfo::getCity, dto.getCity());//根据城市查询
        //其他的查询条件，学员自己尝试添加

        //3 执行查询
        pages = userInfoMapper.selectPage(pages, wrapper);
        return pages;
    }

    @Override
    public List<UserInfo> findByIds(List<Long> ids) {
        return userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class)
                .in(UserInfo::getId, ids));
    }

    //根据好友的ids 和 昵称的关键词  分页查询好友的用户详情
    @Override
    public List<UserInfo> findByPage(List<Long> ids, String keyword, Integer page, Integer pagesize) {
        //1 封装分页对象
        Page<UserInfo> pages = new Page<>(page, pagesize);

        //2 封装查询条件
        LambdaQueryWrapper<UserInfo> wrapper = Wrappers.lambdaQuery(UserInfo.class)
                .in(UserInfo::getId, ids)
                .like(StringUtils.isNotBlank(keyword), UserInfo::getNickname, keyword);

        //3 执行查询
        pages = userInfoMapper.selectPage(pages, wrapper);

        //4 返回结果
        return pages.getRecords();
    }

    //根据ids和性别查询用户详情
    @Override
    public List<UserInfo> findByIds(List<Long> ids, String gender) {
        return userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class)
                .in(UserInfo::getId, ids)
                .eq(StringUtils.isNotBlank(gender), UserInfo::getGender, gender));
    }

    //分页查询用户详情
    @Override
    public Page<UserInfo> findByPage(Integer page, Integer pagesize) {
        //1 封装分页对象
        Page<UserInfo> pages = new Page<>(page, pagesize);

        //2 执行查询
        pages = userInfoMapper.selectPage(pages, null);

        return pages;
    }

    //查询多个用户的用户信息
    public Map<Long, UserInfo> findbyids(List<Long> userIds) {
        //判断是否为空
        if (CollUtil.isEmpty(userIds)) {
            return null;
        }
        //构造条件
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.in("id", userIds);
        List<UserInfo> selectList = userInfoMapper.selectList(wrapper);
        //转换为map
        Map<Long, UserInfo> map = new HashMap<>();
        for (UserInfo info : selectList) {
            map.put(info.getId(), info);
        }
        //最终返回
        return map;
    }


}
